import {
  Controller,
  Get,
  HttpException,
  HttpStatus,
  Param,
  Query,
} from '@nestjs/common';
import { ChatroomService } from './chatroom.service';
import { RequireLogin, UserInfo } from 'src/custom.decorator';

@Controller('chatroom')
@RequireLogin()
export class ChatroomController {
  constructor(private readonly chatroomService: ChatroomService) {}

  @Get('create-one-to-one')
  async oneToNone(
    @Query('friendId') friendId: number,
    @UserInfo('userId') userId: number,
  ) {
    if (!friendId) {
      throw new HttpException(`friendId不能为空`, HttpStatus.BAD_REQUEST);
    }
    return this.chatroomService.createOneToOneChatroom(friendId, userId);
  }

  @Get('create-group')
  group(@Query('name') name: string, @UserInfo('userId') userId: number) {
    if (!name) {
      throw new HttpException(`name不能为空`, HttpStatus.BAD_REQUEST);
    }
    return this.chatroomService.createGroupChatroom(name, userId);
  }

  @Get('list')
  @RequireLogin()
  list(@UserInfo('userId') userId: number) {
    return this.chatroomService.list(userId);
  }

  @Get('members')
  members(@Query('chatroomId') chatroomId: number) {
    if (!chatroomId) {
      throw new HttpException(`chatroomId不能为空`, HttpStatus.BAD_REQUEST);
    }
    return this.chatroomService.members(chatroomId);
  }

  @Get('info/:id')
  info(@Param('id') id: number) {
    if (!id) {
      throw new HttpException(`Id不能为空`, HttpStatus.BAD_REQUEST);
    }
    return this.chatroomService.info(id);
  }

  @Get('join/:id')
  join(@Param('id') id: number, @UserInfo('userId') userId: any) {
    if (!id) {
      throw new HttpException('id 不能为空', HttpStatus.BAD_REQUEST);
    }
    return this.chatroomService.join(id, userId);
  }

  @Get('quit/:id')
  quit(@Param('id') id: number, @UserInfo('userId') userId: any) {
    if (!id) {
      throw new HttpException('id 不能为空', HttpStatus.BAD_REQUEST);
    }
    return this.chatroomService.quit(id, userId);
  }
}
